package com.etc.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.Blog;
import com.etc.entity.Restaurant;
import com.etc.service.BlogService;
import com.etc.utils.AjaxLogicBean;
import com.etc.utils.PageData;

@Controller
@RequestMapping(value = "Userapi")
public class BlogController {

	@Resource(name = "blogService")
	private BlogService blogService;

	@RequestMapping(value = "listBlogToBefore00")
	public String listBlog(Model model) {
		List<Blog> list = blogService.getAllBlog();
		model.addAttribute("list", list);
		return "user/blog";
	}

	@RequestMapping(value = "listBlogTwo")
	public String listBlog02(Integer page, Integer pageSize, String content, Model model) {

		if (content == null) {
			content = "";
		}

		PageData<Blog> pd = blogService.selectAllBlogByPage(1, 6, content);

		List<Blog> list = pd.getData();
		model.addAttribute("list", list);

		list.forEach(System.out::println);

		return "user/blog";

	}

	@RequestMapping(value = "listBlogSingle00")
	public String listBlogSingle01(int bid) {
		System.out.println("bid************:" + bid);
		return "user/blog-single";

	}
	
	
	
	@RequestMapping("getblogbypage")
	public String getRestaurantBypage(Integer page,Integer limit,Model model){
		if(page==null) {
			page=1;
		}
		if(limit==null) {
			limit=9;
		}
		PageData<Blog> pd = new PageData<>();
		List<Blog> list = blogService.getBlogByPage(page,limit);
		int counts=blogService.getcount();
		int totalpage=(counts/limit)+1;
		pd.setData(list);
		pd.setLimit(limit);
		pd.setPage(page);
		pd.setCount(totalpage);
		model.addAttribute("pd", pd);
		return "user/blog";
	}

	@RequestMapping(value="getblog")
	@ResponseBody
	public  AjaxLogicBean<Blog>getRestaurant() {
		List<Blog> list = blogService.getblogs();
		System.out.println(list);
		AjaxLogicBean<Blog> al= new AjaxLogicBean<>();
		al.setData(list);
		return al;
	}
	
}
